package com.bltec.emergency.controller.foreignController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.ForeignSorties;
import com.bltec.emergency.service.foreignService.ForeignSortiesService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/foreignSorties")
@RequiresRoles("superManger")
public class ForeignSortiesController {
    @Resource
    private ForeignSortiesService foreignSortiesService;
    @Resource
    private UserService userService;

    @RequestMapping("getAllForeignSorties")
    public ResponseJson getForeignSorties() {
        try {
            List<ForeignSorties> foreignSorties = foreignSortiesService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(foreignSorties));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /*     private String helicopterName;
    private BigDecimal dailyFee;
    private BigDecimal fuelPrice;
    private String rescueArea;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date rescueBeginTime;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date rescueEndTime;
    private BigDecimal rescueFuel;
    private Long rescueNum;*/
    @RequestMapping("addForeignSorties")
    public ResponseJson addForeignSorties(String helicopterName, BigDecimal dailyFee, BigDecimal fuelPrice, String rescueArea, Date rescueBeginTime, Date rescueEndTime,BigDecimal rescueFuel,Long rescueNum) {
        try {
            ForeignSorties foreignSorties = new ForeignSorties();
            foreignSorties.setHelicopterName(helicopterName);
            foreignSorties.setDailyFee(dailyFee);
            foreignSorties.setFuelPrice(fuelPrice);
            foreignSorties.setRescueArea(rescueArea);
            foreignSorties.setRescueBeginTime(rescueBeginTime);
            foreignSorties.setRescueEndTime(rescueEndTime);
            foreignSorties.setRescueFuel(rescueFuel);
            foreignSorties.setRescueNum(rescueNum);
            foreignSorties.setEnable(true);
            foreignSorties.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            foreignSorties.setCreateTime(DateUtil.dateNow());
            foreignSortiesService.add(foreignSorties);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }


    @RequestMapping("updateForeignSorties")
    public ResponseJson updateForeignSorties(Long id,String helicopterName, BigDecimal dailyFee, BigDecimal fuelPrice, String rescueArea, Date rescueBeginTime, Date rescueEndTime,BigDecimal rescueFuel,Long rescueNum) {
        try {
            ForeignSorties foreignSorties = foreignSortiesService.getForeignSortiesById(id);
            foreignSorties.setHelicopterName(helicopterName);
            foreignSorties.setDailyFee(dailyFee);
            foreignSorties.setFuelPrice(fuelPrice);
            foreignSorties.setRescueArea(rescueArea);
            foreignSorties.setRescueBeginTime(rescueBeginTime);
            foreignSorties.setRescueEndTime(rescueEndTime);
            foreignSorties.setRescueFuel(rescueFuel);
            foreignSorties.setRescueNum(rescueNum);
            foreignSorties.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            foreignSorties.setUpdateTime(DateUtil.dateNow());
            foreignSortiesService.updateForeignSortiesById(foreignSorties);
            return new ResponseJson(Constant.OK, JSON.toJSON(foreignSorties));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }


    @RequestMapping("deleteForeignSorties")
    public ResponseJson setDisable(Long id) {
        if (null != foreignSortiesService.getForeignSortiesById(id)) {
            if (foreignSortiesService.deleteForeignSortiesById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}